﻿using MemberManager.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace MemberManager.Common.Classes
{
    public class CSVHelper
    {

        private PropertyInfo[] GetPropertyInfoArray()
        {
            PropertyInfo[] props = null;
            try
            {
                Type type = typeof(AccountInfo);
                object obj = Activator.CreateInstance(type);
                props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            }
            catch (Exception ex)
            { }
            return props;
        }

        /// <summary>
        /// Save the List data to CSV file
        /// </summary>
        /// <param name="studentList">data source</param>
        /// <param name="filePath">file path</param>
        /// <returns>success flag</returns>
        public bool SaveDataToCSVFile(List<AccountInfo> accountList, string filePath)
        {
            bool successFlag = true;

            StringBuilder strColumn = new StringBuilder();
            StringBuilder strValue = new StringBuilder();
            StreamWriter sw = null;
            PropertyInfo[] props = GetPropertyInfoArray();

            try
            {
                sw = new StreamWriter(filePath);
                for (int i = 0; i < props.Length; i++)
                {
                    strColumn.Append(props[i].Name);
                    strColumn.Append(",");
                }
                strColumn.Remove(strColumn.Length - 1, 1);
                sw.WriteLine(strColumn);    //write the column name

                for (int i = 0; i < accountList.Count; i++)
                {
                    strValue.Remove(0, strValue.Length); //clear the temp row value
                    strValue.Append(accountList[i].DomainName);
                    strValue.Append(",");
                    strValue.Append(accountList[i].AccountName);
                    strValue.Append(",");
                    strValue.Append(accountList[i].IsDefaultFloder);
                    strValue.Append(",");
                    strValue.Append(accountList[i].AccountPwd);
                    sw.WriteLine(strValue); //write the row value
                }
            }
            catch (Exception ex)
            {
                successFlag = false;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Dispose();
                }
            }

            return successFlag;
        }


        /// <summary>
        /// 读取CSV文件通过文本格式
        /// </summary>
        /// <param name="strpath"></param>
        /// <returns></returns>
        public List<Models.AccountInfo> ReadDataFromCSVFile(string strpath)
        {
            List<Models.AccountInfo> list = new List<AccountInfo>();

            int intColCount = 0;
            bool blnFlag = true;
            DataTable mydt = new DataTable("myTableName");

            DataColumn mydc;
            DataRow mydr;

            string strline;
            string[] aryline;

            System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);

            while ((strline = mysr.ReadLine()) != null)
            {
                aryline = strline.Split(',');

                if (blnFlag)
                {
                    blnFlag = false;
                    intColCount = aryline.Length;
                    for (int i = 0; i < aryline.Length; i++)
                    {
                        mydc = new DataColumn(aryline[i]);
                        mydt.Columns.Add(mydc);
                    }
                }

                mydr = mydt.NewRow();
                for (int i = 0; i < intColCount; i++)
                {


                    mydr[i] = aryline[i];
                }
                mydt.Rows.Add(mydr);

                //如果是第一行  列明
                if(mydt.Rows[mydt.Rows.Count - 1]["DomainName"].ToString() == "DomainName")
                {
                    continue;
                }

                Models.AccountInfo model = new AccountInfo();
                model.DomainName = mydt.Rows[mydt.Rows.Count - 1]["DomainName"].ToString();
                model.AccountName = mydt.Rows[mydt.Rows.Count - 1]["AccountName"].ToString();
                model.AccountPwd = mydt.Rows[mydt.Rows.Count - 1]["AccountPwd"].ToString();
                model.IsDefaultFloder = Convert.ToBoolean(mydt.Rows[mydt.Rows.Count - 1]["IsDefaultFloder"]);
                list.Add(model);
            }

            return list;
        }
    }
}